﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NetAssist.IO;
//using System.Collections;

namespace NetAssistTests
{
  [DeploymentItem("Resources\\instructions.txt")]
  [TestClass()]
  public class TextFileTests
  {
    private const int READ_SIZE = 100;

    [TestMethod()]
    public void TextFileTest1()
    {
      string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
      path = Path.Combine(path, "instructions.txt");

      StringBuilder contentBuilder = new StringBuilder();
      IEnumerable<TextFileBlock> textFileReader = TextFile.ReadFile(path, READ_SIZE);
      foreach (TextFileBlock block in textFileReader)
        contentBuilder.Append(block);

      string content = File.OpenText(path).ReadToEnd();
      Assert.IsTrue(content == contentBuilder.ToString());
      Assert.IsTrue(textFileReader.First().BlockIndex == 0);

      TextFileBlock lastBlock = textFileReader.Last();

      Assert.IsTrue(lastBlock.BlockIndex == 80);
      Assert.IsTrue(lastBlock.EndPosition == 8090);
      Assert.IsTrue(lastBlock.Length == 90);
      Assert.IsTrue(lastBlock.StartPosition == 8000);
      Assert.IsTrue(lastBlock.StartPosition / lastBlock.BlockIndex == READ_SIZE);

      Assert.IsTrue(content.EndsWith(TextFileBlock.FromTextFileBlock(lastBlock)));
    }

    [TestMethod()]
    public void TextFileTest2()
    {
      string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
      path = Path.Combine(path, "instructions.txt");

      IEnumerable<TextFileBlock> tfr1 = TextFile.ReadFile(path, TextReader);
      IEnumerable<TextFileBlock> tfr2 = TextFile.ReadFile(path, READ_SIZE);

      string block1 = tfr1.Skip(3).First().ToString();
      string block2 = tfr2.Skip(3).First().ToString();
      Assert.IsTrue(block1 == block2);
    }

    private string TextReader(StreamReader source)
    {
      char[] arr = new char[READ_SIZE];
      source.Read(arr, 0, READ_SIZE);
      return new string(arr);
    }
  }
}
